let requestUrl;
let list = [15, 30, 50, 100];

function addHandler() {
	let areaId = $("#area_id").combobox("getValue");

	if(areaId) {
		$("#areaId").combobox("setValue", areaId);
	}

	requestUrl = "/server/insert";

	$("#server_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#server_list").datagrid("getSelected");

	if(rowData) {
		requestUrl = "/server/updateById";

		$("#id").val(rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#areaId").combobox("setValue", rowData.areaId);

		$("#server_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function deleteHandler(id) {
	$.messager.confirm("提示", "是否确认删除？", function(bool) {
		if (bool) {
			ajaxPost("/server/deleteById", {
				id: id
			}, function(response) {
				showMsg(response.message);

				$("#server_list").datagrid("reload");
			}, error);
		}
	});
}

$(document).ready(function() {

	$("#area_id").combobox({
		url: "/area/selectAll",
		width: 120,
        valueField: "id",
        textField: "name",
		panelHeight: "auto",
		prompt: "--选择大区--"
	});
	
	$("#_name_").textbox({
		width: 120,
		prompt: "服务器名称"
	});
	
	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let areaId = $("#area_id").combobox("getValue");
		let name = $("#_name_").textbox("getValue");

		$("#server_list").datagrid("load", {
			areaId: areaId,
			name: name
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	}).attr("title", "清空搜索条件");

	// 服务器名
	$("#name").textbox({
		width: 120,
		required: true
	});
	
	// 大区
	$("#areaId").combobox({
		url: "/area/selectAll",
        valueField: "id",
		textField: "name",
		width: 120,
		required: true,
		editable: false,
		panelHeight: "auto"
	});
	
	$("#server_dialog").dialog({
		title: "服务器",
		width: 500,
		height: 300,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#server_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(response) {
						showMsg(response.message);

						$(selector).form("clear");
						$("#server_dialog").dialog("close");
						$("#server_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#server_dialog").dialog("close");
				$("#server_form").form("clear");
			}
		}]
	});
	
	// 服务器列表
	$("#server_list").datagrid({
		url: "/server/selectByPage",
		title: "服务器列表",
		height: table_height_with_search,
		pagination: true,
		pageList: list,
		pageSize: list[0],
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}],
		columns: [[
			{field: "id", title: "编号", align: "center"},
			{field: "name", title: "服务器名称", align: "center", width: 100},
			{field: "areaId", title: "游戏大区", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/area/selectById", {
						id: value
					}, function(res) {
						$("#area_" + rowIndex).html(res.data.name);
					}, error);
					
					return "<span id='area_" + rowIndex + "'></span>";
				}
			},
			{field: "right", title: "操作", align:"center"
				, formatter: function(value, rowData, rowIndex) {
					return "<a href='javascript:void(0);' onclick='deleteHandler(" + rowData.id + ")'>"
						+ "<img alt='' title='删除' src='/css/themes/icons/delete.png' /></a>";
				}
			}
		]]
	});

});